package com.haiertvbic.tvnow.service;

import android.content.Context;
import android.os.Looper;

import com.haier.tvmidlware.TvEpgMidwareService;
import com.haiertvbic.tvnow.baseactivity.SwitchActivity;
import com.haiertvbic.tvnow.protocol.CAEProtocol;
import com.haiertvbic.tvnow.util.Constant;
import com.haiertvbic.tvnow.util.SLog;
import com.haiertvbic.tvnow.util.Util;
import com.mstar.android.tv.TvChannelManager;
import com.mstar.android.tvapi.common.ChannelManager;
import com.mstar.android.tvapi.common.TvManager;
import com.mstar.android.tvapi.common.exception.TvCommonException;

public class SwitchChannel {
	protected static final String TAG = "FavChannel";
	private static int switchCode;

	public static void CAESwitchChannel(final int cid,
			final SwitchChanellCallback switchChanellCallback) {
		new Thread() {
			@Override
			public void run() {
				super.run();
				switchCode = CAEProtocol.changeChannel(Integer.valueOf(cid), 0);
				int num = Integer.valueOf(cid);
				// num = VDService.getDTVIdWithWholeCountryId(1,
				// (short)num);//请求全国频道id
				SLog.i("switchchannel", switchCode + "");
				if (switchCode == 4004500) {
					switchChanellCallback.returnData(1, switchCode, num);
				} else {
					switchChanellCallback.returnData(1, switchCode, -1);
				}
			}
		}.start();

	}

	public static void DTVSwitchChannel(final Context context,
			final int channelId,
			final SwitchChanellCallback switchChanellCallback) {
		new Thread() {
			@Override
			public void run() {
				try {
					Looper.prepare();
				} catch (Exception e) {
				}
				if (Constant.TvPlatform.contains("6A818")) {
					TvChannelManager tcm = TvChannelManager.getInstance();
					tcm.programSel(channelId, (short) 28);
					tcm.playDtvCurrentProgram();
					TvChannelManager.getInstance().playDtvCurrentProgram();
					switchChanellCallback.returnData(0,
							SwitchActivity.DTV_801_CHANGE, channelId);
					// mstarChangeChannel(channelId, switchChanellCallback);
				} else {
					TvEpgMidwareService tvservice = new TvEpgMidwareService(
							context);
					tvservice.dtvProgramSelByChannelNum(channelId);
					switchChanellCallback.returnData(0,
							SwitchActivity.DTV_801_CHANGE, channelId);
				}
			}

			/**
			 * mstar换台方法，即818平台的换台方法
			 * 
			 * @param channelId
			 * @param switchChanellCallback
			 */
			private void mstarChangeChannel(final int channelId,
					final SwitchChanellCallback switchChanellCallback) {
				TvChannelManager tcm = TvChannelManager.getInstance();
				tcm.programSel(channelId, (short) 28);
				switchChanellCallback.returnData(0,
						SwitchActivity.DTV_801_CHANGE, channelId);
				// ChannelManager cm =
				// TvManager.getInstance().getChannelManager();
				// try {
				// int current = cm.getCurrChannelNumber();
				// if (current == channelId) {
				// switchChanellCallback.returnData(0,
				// SwitchActivity.THE_SAME_CID, -1);
				// return;
				// }
				// } catch (TvCommonException e) {
				// SLog.i(TAG, e.toString());
				// }
				//
				// try {
				// // 28表示dtv
				// cm.selectProgram(channelId, (short) 28, 0x00);
				// switchChanellCallback.returnData(0, 4004500, channelId);
				// } catch (TvCommonException e) {
				// switchChanellCallback.returnData(0,
				// SwitchActivity.DTV_CHANGE_FAILED, channelId);
				// }
			}

		}.start();

	}
}
